package com.cdtu.sys.controller;

import com.cdtu.sys.domain.UserVo;
import com.cdtu.sys.service.IUserService;
import com.cdtu.sys.utils.DataGridView;
import com.cdtu.sys.utils.ResultObj;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userService;

    /**
     * 加载用户列表/模糊查询
     */
    @GetMapping("/loadAllUser")
    public DataGridView loadAllUser(UserVo userVo) {
        return userService.queryAllUser(userVo);
    }

    /**
     * 添加用户
     */
    @PostMapping("/addUser")
    public ResultObj addUser(UserVo userVo) {
        try {
            userService.addUser(userVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除用户
     */
    @GetMapping("/deleteUser")
    public ResultObj deleteUser(UserVo userVo) {
        try {
            userService.deleteUser(userVo);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加用户
     */
    @PostMapping("/updateUser")
    public ResultObj updateUser(UserVo userVo) {
        try {
            userService.updateUser(userVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除用户
     * 参数：ids
     */
    @GetMapping("/deleteBatchUser")
    public ResultObj deleteBatchUser(UserVo userVo) {
        try {
            userService.deleteBatchUser(userVo);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 重置密码
     * 参数：userid
     */
    @PostMapping("/resetUserPwd")
    public ResultObj resetUserPwd(UserVo userVo) {
        try {
            userService.resetUserPwd(userVo);
            return ResultObj.RESET_SUCCESS;
        } catch (Exception e) {
            return ResultObj.RESET_ERROR;
        }
    }

    /**
     * 初始化角色分配
     * 参数：userid
     * 返回：角色的网格视图
     */
    @GetMapping("/initUserRole")
    public DataGridView initUserRole(UserVo userVo) {

        return userService.initUserRole(userVo);

    }

    /**
     * 分配角色
     * 参数：userid，ids
     */
    @PostMapping("/saveUserRole")
    public ResultObj saveUserRole(UserVo userVo) {
        try {
            userService.saveUserRole(userVo);
            return ResultObj.RESET_SUCCESS;
        } catch (Exception e) {
            return ResultObj.RESET_ERROR;
        }
    }

}
